/**
 * Created by 邵亚楠 on 2018/9/8.
 */
var sessionId = sessionStorage.getItem('sessionId');
$(function(){
    $.ajax({
        url:'https://www.bjdeshangyuan.com/book/api/comment/allCommentList',
        type:'POST',
        data:{'sessionid':sessionId,'bookId':-1},
        success:function(r){
            var newData = r.data
            var newDataList = r.data.commentList
            //console.log(newDataList)
            if(r.code ==='USR000'){
                $('.count').text(newData.commentCounts)
                for(var i=0;i<newDataList.length;i++){
                    var commentList = newDataList[i]
                    var commentId = commentList.id
                    var isLike = newDataList[i].isLike
                    //console.log(isLike)
                    //console.log(commentId)
                    var icon_class_name = isLike == 0 ? '' : 'bgc';
                    var html =
                '<div class="comment_item">'+

                        '<div class="pic">'+
                        '<img src="'+commentList.photo+'" alt="">'+
                        '</div>'+
                        '<div class="comment-detail">'+
                        '<p class="user_box"><span>'+commentList.userName+'</span><span class="like_box" onclick="getLike('+commentId+')" data-id='+commentId+'><span class="like '+icon_class_name+'"></span><span class="count">'+commentList.goods+'</span></span></p>'+

                    '<p class="text">'+commentList.bookcontent+'</p>'+
                        '</div>'+
                        '</div>'

                    $('.comment_list').append(html)
                }

            }
        }
    })

    $('.back').click(function(){
        window.history.back();
    })

})

//评论点赞
function getLike(id){
    $.ajax({
        url:'https://www.bjdeshangyuan.com/book/api/comment/apiAddGood',
        type:'POST',
        data:{'sessionid':sessionId,'commentId':id},
        success:function(r){
            //console.log(r.data.thumbUpTimes)
            if(r.code =="USR000"){
                //layer.msg('点赞成功')
                var Count = r.data.thumbUpTimes
                var $ref = $('.like_box')
                var isLike = r.data.isLike
                console.log(isLike)
                $ref.each(function(){
                    var refs = $(this)
                    if(refs.attr('data-id') == id){
                        if(isLike == 0){
                            refs.find('.like').removeClass('bgc')
                        }else{
                            refs.find('.like').addClass('bgc')

                        }
                        refs.find('.count').text(Count)
                    }
                })
            }

        }
    })
}